﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Bussines;

namespace Bussines
{
    public class clNYUGDIJAK
    {
        private SqlConnection sc;
        private int _PRT_ID = -1;
        TBLObj pBLObj = new TBLObj();

        SqlDataAdapter adapterNYUGDIJAK;

        DataSet dsNYUGDIJAK;

        public clNYUGDIJAK(TBLObj iBLObj, int iPRT_ID)
        {
            pBLObj = iBLObj;
            sc = iBLObj.sqlConnection;
            _PRT_ID = iPRT_ID;
        }

        public DataSet GetNYUGDIJAK()
        {
            dsNYUGDIJAK = new DataSet();
            adapterNYUGDIJAK = new SqlDataAdapter();

            adapterNYUGDIJAK.SelectCommand = new SqlCommand("SELECT " +
                " NYG_ID, NYG_TIPUS, NYG_PRT_ID, NYG_INT_ID, NYG_HOZAM, NYG_MEGJEGYZES, NYG_CREATED, NYG_CREATED_BY, NYG_MODIFIED, NYG_MODIFIED_BY, " +
                " INT_MEGNEVEZES, " +
                " TPS_MEGNEVEZES " +
                "FROM NYUGDIJAK " +
                "INNER JOIN INTEZMENYEK ON INT_ID = NYG_INT_ID " +
                "INNER JOIN TIPUS ON TPS_TIPUSKOD = NYG_TIPUS AND TPS_KATEGORIA = 'NYG' " +
                "WHERE NYG_PRT_ID = " + _PRT_ID.ToString(), sc);
            adapterNYUGDIJAK.Fill(dsNYUGDIJAK, "NYUGDIJAK");
            /*  insertCommand();
            updateCommandCACT();*/

            return dsNYUGDIJAK;
        }

        public bool SaveNYUGDIJAK(int iNYG_ID, int iNYG_TIPUS, int iNYG_PRT_ID, int iNYG_INT_ID, double iNYG_HOZAM, 
                        string iNYG_MEGJEGYZES, DateTime iNYG_CREATED, int iNYG_CREATED_BY, DateTime iNYG_MODIFIED, int iNYG_MODIFIED_BY, ref string oErr)
        {
            SqlCommand cmd = new SqlCommand();
            SqlCommand cmd_insert = new SqlCommand();

            oErr = "";

            cmd.Connection = sc;
            cmd_insert.Connection = sc;

            cmd.CommandType = CommandType.Text;
            cmd_insert.CommandType = CommandType.Text;

            cmd.CommandText = "UPDATE NYUGDIJAK SET " +
                " NYG_TIPUS = @NYG_TIPUS, " +
                " NYG_PRT_ID = @NYG_PRT_ID, " +
                " NYG_INT_ID = @NYG_INT_ID, " +
                " NYG_HOZAM = @NYG_HOZAM, " +
                " NYG_MEGJEGYZES = @NYG_MEGJEGYZES, " +
                " NYG_MODIFIED = @NYG_MODIFIED, " +
                " NYG_MODIFIED_BY = @NYG_MODIFIED_BY " +
                "WHERE " +
                " NYG_ID = @NYG_ID ";

            cmd.Parameters.Add(new SqlParameter("NYG_ID", SqlDbType.Int));
            cmd.Parameters.Add(new SqlParameter("NYG_TIPUS", SqlDbType.Int));
            cmd.Parameters.Add(new SqlParameter("NYG_PRT_ID", SqlDbType.Int));
            cmd.Parameters.Add(new SqlParameter("NYG_INT_ID", SqlDbType.Int));
            cmd.Parameters.Add(new SqlParameter("NYG_HOZAM", SqlDbType.Money));
            cmd.Parameters.Add(new SqlParameter("NYG_MEGJEGYZES", SqlDbType.VarChar));
            cmd.Parameters.Add(new SqlParameter("NYG_MODIFIED", SqlDbType.DateTime));
            cmd.Parameters.Add(new SqlParameter("NYG_MODIFIED_BY", SqlDbType.Int));

            cmd.Parameters["NYG_ID"].Value = iNYG_ID;
            cmd.Parameters["NYG_TIPUS"].Value = iNYG_TIPUS;
            cmd.Parameters["NYG_PRT_ID"].Value = iNYG_PRT_ID;
            cmd.Parameters["NYG_INT_ID"].Value = iNYG_INT_ID;
            cmd.Parameters["NYG_HOZAM"].Value = iNYG_HOZAM;
            cmd.Parameters["NYG_MEGJEGYZES"].Value = iNYG_MEGJEGYZES;
            cmd.Parameters["NYG_MODIFIED"].Value = iNYG_MODIFIED;
            cmd.Parameters["NYG_MODIFIED_BY"].Value = iNYG_MODIFIED_BY;
            

            cmd_insert.CommandText = "INSERT INTO NYUGDIJAK " +
                "(NYG_TIPUS, NYG_PRT_ID, NYG_INT_ID, NYG_HOZAM, NYG_MEGJEGYZES, NYG_CREATED, NYG_CREATED_BY, NYG_MODIFIED, NYG_MODIFIED_BY) " +
                "VALUES " +
                "(@NYG_TIPUS, @NYG_PRT_ID, @NYG_INT_ID, @NYG_HOZAM, @NYG_MEGJEGYZES, @NYG_CREATED, @NYG_CREATED_BY, @NYG_MODIFIED, @NYG_MODIFIED_BY) ";

            cmd_insert.Parameters.Add(new SqlParameter("NYG_TIPUS", SqlDbType.Int));
            cmd_insert.Parameters.Add(new SqlParameter("NYG_PRT_ID", SqlDbType.Int));
            cmd_insert.Parameters.Add(new SqlParameter("NYG_INT_ID", SqlDbType.Int));
            cmd_insert.Parameters.Add(new SqlParameter("NYG_HOZAM", SqlDbType.Money));
            cmd_insert.Parameters.Add(new SqlParameter("NYG_MEGJEGYZES", SqlDbType.VarChar));
            cmd_insert.Parameters.Add(new SqlParameter("NYG_MODIFIED", SqlDbType.DateTime));
            cmd_insert.Parameters.Add(new SqlParameter("NYG_MODIFIED_BY", SqlDbType.Int));
            cmd_insert.Parameters.Add(new SqlParameter("NYG_CREATED", SqlDbType.DateTime));
            cmd_insert.Parameters.Add(new SqlParameter("NYG_CREATED_BY", SqlDbType.Int));

            cmd_insert.Parameters["NYG_TIPUS"].Value = iNYG_TIPUS;
            cmd_insert.Parameters["NYG_PRT_ID"].Value = iNYG_PRT_ID;
            cmd_insert.Parameters["NYG_INT_ID"].Value = iNYG_INT_ID;
            cmd_insert.Parameters["NYG_HOZAM"].Value = iNYG_HOZAM;
            cmd_insert.Parameters["NYG_MEGJEGYZES"].Value = iNYG_MEGJEGYZES;
            cmd_insert.Parameters["NYG_MODIFIED"].Value = iNYG_MODIFIED;
            cmd_insert.Parameters["NYG_MODIFIED_BY"].Value = iNYG_MODIFIED_BY;
            cmd_insert.Parameters["NYG_CREATED"].Value = iNYG_CREATED;
            cmd_insert.Parameters["NYG_CREATED_BY"].Value = iNYG_CREATED_BY;

            try
            {
                if (iNYG_ID == -1)
                {
                    cmd_insert.ExecuteNonQuery();
                }
                else
                {
                    cmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception e)
            {

                string s = "Hiba a nyugdíj mentése közben!";
                Utilities.LogError(pBLObj, e, "clNYUGDIJ", s);
                oErr = s;
                return false;
            }
        }

    }
}
